package com.bestom.videoplay.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bestom.videoplay.beans.CommandBean;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class ScreenReceiver extends BroadcastReceiver {

    // 当我们进行屏幕锁屏和解锁时，执行下面的方法
    @Override
    public void onReceive(Context context, Intent intent) {
        // 获取当前广播的事件类型
        String action = intent.getAction();

        if (Intent.ACTION_SCREEN_OFF.equals(action)) {
            writeDataToNode("1");
            Log.d("ScreenReceiver","息屏");
        } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
            writeDataToNode("0");
            Log.d("ScreenReceiver","亮屏");
        }else if (Intent.ACTION_SHUTDOWN.equals(action)){
            writeDataToNode("1");
            Log.d("ScreenReceiver","关机");
        }else if (Intent.ACTION_BOOT_COMPLETED.equals(action)){
            writeDataToNode("0");
            Log.d("ScreenReceiver","开机");
        }
    }
    String path = "/sys/class/gpio/gpio39/value";

    private void writeDataToNode(String value) {
        try {
            BufferedWriter bufWriter = new BufferedWriter(new FileWriter(path));
            // 写入数据
            bufWriter.write(value);
            bufWriter.close();
            Log.d("ScreenReceiver","改写电源状态节点成功!" + value);
        } catch (IOException e) {

            e.printStackTrace();
            Log.d("ScreenReceiver","改写电源状态节点失败!");
        }
    }
}
